//
//  MineHeaderView.m
//  KingSelected
//
//  Created by 杨鹏 on 2023/10/28.
//

#import "MineHeaderView.h"

@interface MineHeaderView ()

@property (nonatomic, strong) UIImageView *avatarImageV;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *idLabel;
@property (nonatomic, strong) UIButton *editBtn;
@property (nonatomic, strong) UIButton *qrcodeBtn;

@end

@implementation MineHeaderView

- (void)initViews {
    [self addSubview:self.avatarImageV];
    [self addSubview:self.nameLabel];
    [self addSubview:self.idLabel];
    [self addSubview:self.editBtn];
    [self addSubview:self.qrcodeBtn];
    
    [self.avatarImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(16);
        make.width.height.mas_equalTo(50);
        make.centerY.mas_equalTo(0);
    }];
    [self.qrcodeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(-16);
        make.width.height.mas_equalTo(30);
        make.centerY.mas_equalTo(0);
    }];
    [self.editBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.qrcodeBtn.mas_leading).offset(-10);
        make.centerY.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(70, 24));
    }];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.avatarImageV.mas_trailing).offset(10);
        make.trailing.mas_equalTo(self.editBtn.mas_leading).offset(-10);
        make.top.mas_equalTo(self.avatarImageV.mas_top).offset(4);
    }];
    [self.idLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.nameLabel);
        make.bottom.mas_equalTo(self.avatarImageV.mas_bottom).offset(-4);
    }];
}

- (void)setModel:(UserModel *)model {
    _model = model;
    [self.avatarImageV sd_setImageWithURL:[NSURL URLWithString:model.avatar] placeholderImage:[UIImage imageNamed:@"avatar"]];
    self.nameLabel.text = model.nickName;
    self.idLabel.text = [NSString stringWithFormat:@"ID:%@",model.id];
}

- (UIImageView *)avatarImageV {
    if (!_avatarImageV) {
        _avatarImageV = [[UIImageView alloc] init];
        _avatarImageV.contentMode = UIViewContentModeScaleAspectFill;
        _avatarImageV.clipsToBounds = YES;
        _avatarImageV.layer.cornerRadius = 25;
    }
    return _avatarImageV;
}
- (UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.textColor = kRGB(0x333333);
        _nameLabel.font = kFontCarter(16);
        _nameLabel.textAlignment = NSTextAlignmentLeft;
        [_nameLabel sizeToFit];
    }
    return _nameLabel;
}
- (UILabel *)idLabel {
    if (!_idLabel) {
        _idLabel = [[UILabel alloc] init];
        _idLabel.textColor = kRGB(0x999999);
        _idLabel.font = kFontRegula(12);
        [_idLabel sizeToFit];
    }
    return _idLabel;
}
- (UIButton *)qrcodeBtn {
    if (!_qrcodeBtn) {
        _qrcodeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_qrcodeBtn setImage:[UIImage imageNamed:@"mine_qr"] forState:UIControlStateNormal];
        @weakify(self)
        [[_qrcodeBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self)
            if (self.showQrcodeBlock) {
                self.showQrcodeBlock();
            }
        }];
    }
    return _qrcodeBtn;
}
- (UIButton *)editBtn {
    if (!_editBtn) {
        _editBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_editBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_editBtn setTitle:@"编辑资料" forState:UIControlStateNormal];
        _editBtn.backgroundColor = kBgColor;
        _editBtn.titleLabel.font = kFontMediium(12);
        _editBtn.layer.cornerRadius = 12;
        _editBtn.layer.masksToBounds = YES;
        @weakify(self)
        [[_editBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self)
            if (self.editUserBlock) {
                self.editUserBlock();
            }
        }];
    }
    return _editBtn;
}
@end
